package org.kongqian.marketing.service.impl;

import org.kongqian.marketing.domain.Business;
import org.kongqian.marketing.domain.BusinessProduct;
import org.kongqian.marketing.domain.Clue;
import org.kongqian.marketing.domain.ClueRemark;
import org.kongqian.marketing.dto.ClueActivityDTO;
import org.kongqian.marketing.dto.ClueBusinessDTO;
import org.kongqian.marketing.mapper.*;
import org.kongqian.marketing.service.IClueService;
import org.kongqian.basic.service.impl.BaseServiceImpl;
import org.kongqian.marketing.vo.PieChartVO;
import org.kongqian.productmanager.domain.Product;
import org.kongqian.productmanager.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kq
 * @since 2022-10-24
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {

    @Autowired
    private ClueActivityMapper clueActivityMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private BusinessProductMapper businessProductMapper;

    @Override
    @Transactional
    public void clueActivitySubmit(ClueActivityDTO dto) {
        clueActivityMapper.deleteById(dto.getClueId());
        clueActivityMapper.clueActivitySubmit(dto);
    }

    @Override
    @Transactional
    public void clueAssignment(Clue clue) {
        //先修改线索状态
        clue.setId(1L);
        //分配人 分配时间
        clue.setEdit_time(new Date());
        clue.setCreate_by(null);//当前登录用户
        //调用修改方法
        clueMapper.updateById(clue);

        //变更线索要备注
        ClueRemark clueRemark = new ClueRemark();
        //线索id给他  创建人 和创建时间  记录操作的人
        clueRemark.setClue_id(clue.getId());
        clueRemark.setCreate_by(null); //当前登录用户的id
        clueRemark.setCreate_time(new Date());
        clueRemark.setNote_content(clue.getFull_name()+"被分配给了："+clue.getSales().getUsername()+"操作人："+"李华");//操作人为当前登录用户
        clueRemarkMapper.insert(clueRemark);
    }

    @Override
    public List<PieChartVO> getPieChart() {
        return clueMapper.getPieChart();
    }


    @Override
    @Transactional
    public void changeBusinessSubmit(ClueBusinessDTO dto) {
        //获取线索和产品的基本信息
        Clue clue = dto.getClue();
        Long productId = dto.getProduct().getId();
        Product product = productMapper.selectById(productId);

        // 修改线索的状态 = 2已转化商机 修改人  修改时间
        clue.setState(2);
        clue.setEdit_by("李华");//当前登录用户
        clue.setEdit_time(new Date());

        clueMapper.updateById(clue);

        // 记录线索操作备注到线索备注表
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setNote_content("李华将"+clue.getFull_name()+"转化为商机");
        clueRemark.setClue_id(clue.getId());
        clueRemark.setCreate_by(null);//当前登录用户
        clueRemark.setCreate_time(new Date());

        clueRemarkMapper.insert(clueRemark);

        // 创建商机 - 新增一条数据到t_mkt_business
        Business business = new Business();
        business.setName(clue.getFull_name()+":被"+"李华转化为商机");//李华为当前登录用户
        business.setClue_id(clue.getId());
        business.setProduct_id(product.getId());
        business.setName(product.getName());
        business.setProduct_price(product.getPrice());
        business.setState(0);//0是商机跟进中

        businessMapper.insert(business);

        // 将产生商机这一刻的产品信息存放到 t_mkt_business_product
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product, businessProduct);
        businessProduct.setId(null);
        businessProduct.setBusiness_id(business.getId());
        businessProductMapper.insert(businessProduct);

    }
}
